
*************************************************
* Summary Stats - Table 1
*************************************************
// Panel A - Facility Level
use "$Data/Temp_Files/Facility_Base",replace
// Generate Indicators
eststo clear
eststo: estpost summarize rating_overall_w rating_survey_w staff_hr_by_resday_w cna_hr_by_resday_w lpn_hr_by_resday_w rn_hr_by_resday_w ///
	totbeds_w adm_w agg_black_w paymcaid_w paymcare_w payprivate_w  ///
	lman_fee_all_totalxw llease_all_totalxw lint_expense_all_totalxw, detail

// T Test
eststo : estpost ttest rating_overall_w rating_survey_w staff_hr_by_resday_w cna_hr_by_resday_w lpn_hr_by_resday_w rn_hr_by_resday_w ///
	totbeds_w adm_w agg_black_w paymcaid_w paymcare_w payprivate_w  ///
	lman_fee_all_totalxw llease_all_totalxw lint_expense_all_totalxw, by(peind) 
	
esttab using "$Output/Table/1A_Summary_Facility.csv" , cells("mean(fmt(%9.2f)) sd(fmt(%9.2f)) count(fmt(%9.3gc)) mu_1(fmt(%9.2f)) N_1(fmt(%9.3gc)) mu_2(fmt(%9.2f)) N_2(fmt(%9.3gc)) p(fmt(%9.4f))") nonumber nodepvar  label replace noobs plain


// Panel B- Person Level
use "$Data/Temp_Files/Person_Base",replace
// Generate Indicators
eststo clear
eststo: estpost summarize age female black white married charles_old Circ Inj Other dual ///
diff_dist_w mortality_90 antipyschotic_ind_level mobility_self_worse ulcer_worse pain_intensity_worse pmt_total_deflated payment_total_post, detail

// T Test
eststo : estpost ttest age female black white married charles_old Circ Inj Other dual ///
diff_dist_w mortality_90 antipyschotic_ind_level mobility_self_worse ulcer_worse pain_intensity_worse pmt_total_deflated payment_total_post, by(peind) 

esttab using "$Output/Table/1B_Summary_Person.csv" , cells("mean(fmt(%9.4f)) sd(fmt(%9.4f)) count(fmt(%9.3gc)) mu_1(fmt(%9.4f)) N_1(fmt(%9.3gc)) mu_2(fmt(%9.4f)) N_2(fmt(%9.3gc)) p(fmt(%9.4f))") ///
nonumber nodepvar label replace noobs plain 

*************************************************
* OLS regressions - Table 2 Panel A
*************************************************
use "$Data/Temp_Files/Person_Base",replace

eststo clear
estimates clear

local mds_initial ADL_self_initial  alzheimers_initial dementia_initial depression_initial 

foreach var of local mds_initial{
	eststo reg`var': reghdfe `var' peind c.age#i.sex married dual, absorb(facid race  bene_hrr#year) tol(1e-6) accel(sd) cluster(facid)
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

*Save to table;	
esttab reg* using "$Output/Table/2_A_OLS_Initial.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean, labels("Observations" "Y-mean") fmt(%9.3gc %9.4f)) 

*************************************************
* OLS regressions - Table 2 Panel B
*************************************************
use "$Data/Temp_Files/Person_Base",replace

local output2 mortality_90 log_pmt log_payment_total_post

eststo clear
estimates clear

foreach var of local output2{
	eststo reg`var': reghdfe `var' peind c.age#i.sex married gp_old* dual, absorb(facid race  bene_hrr#year) tol(1e-6) accel(sd) cluster(facid)
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

	
esttab reg* using "$Output/Table/2_B_OLS_Main.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean, labels("Observations" "Y-mean") fmt(%9.3gc %9.4f)) 



*************************************************
* 2SLS regressions - Table 2 Panel C
*************************************************
use "$Data/Temp_Files/Person_Base",replace

local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/2_C_IV_Regs.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

*************************************************
* Placebo Regressions - Table 2 Panel D
*************************************************
use "$Data/Temp_Files/Person_Base",replace


local output2 mortality_90 log_pmt log_payment_total_post

foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind_fake =  diff_dist_mod_fake diff_dist_sq_fake) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst savefprefix(`var'_)
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
	
esttab reg* using "$Output/Table/2_D_Placebo.csv", label keep(peind_fake) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 


*************************************************
* First Stage Regressions - Table 3
*************************************************
use "$Data/Temp_Files/Person_Base",replace
eststo clear

eststo reg1: reghdfe peind diff_dist_mod diff_dist_sq, ///
absorb(facid bene_hrr#year) tol(1e-6) accel(sd) cluster(facid)
test _b[diff_dist_mod] = _b[diff_dist_sq] = 0
estadd scalar FStat = r(F)
summ peind
estadd scalar mean = r(mean)

eststo reg2: reghdfe peind diff_dist_mod diff_dist_sq c.age#i.sex married gp_old* dual, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid)
test _b[diff_dist_mod] = _b[diff_dist_sq] = 0
estadd scalar FStat = r(F)
summ peind
estadd scalar mean = r(mean)

eststo reg3: reghdfe peind diff_dist_mod diff_dist_sq c.age#i.sex married gp_old* dual ratio_white_ip ratio_renter_ip ratio_below_pvty_ip HH_income_median_ip ma_prop_ind, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid)
test _b[diff_dist_mod] = _b[diff_dist_sq] = 0
estadd scalar FStat = r(F)
summ peind
estadd scalar mean = r(mean)

eststo reg4: reghdfe peind diff_dist_mod diff_dist_sq c.age#i.sex married gp_old* dual, ///
absorb(facid bene_hsa2#year race) tol(1e-6) accel(sd) cluster(facid)
test _b[diff_dist_mod] = _b[diff_dist_sq] = 0
estadd scalar FStat = r(F)
summ peind
estadd scalar mean = r(mean)

esttab reg* using "$Output/Table/3_First_Stage.csv", label keep(diff_dist_mod diff_dist_sq) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "F-Stat") fmt(%9.3gc %9.4f))

*************************************************
* Balance of Patient Chars - Table 4
*************************************************
use "$Data/Temp_Files/Person_Base",replace

// Summary Stats for coefficients
egen diff_dist_median = median(diff_dist_mod)
gen diff_dist_ind = diff_dist_mod > diff_dist_median
// Generate variables in percentage
gen risk_1 = peind
gen risk_2 = age
gen risk_3 = female 
gen risk_4 = black 
gen risk_5 = married 
gen risk_6 = gp_old_1 
gen risk_7 = gp_old_2 
gen risk_8 = gp_old_3 
gen risk_9 = gp_old_4 
gen risk_10 = gp_old_5 
gen risk_11 = gp_old_6 
gen risk_12 = gp_old_7 
gen risk_13 = gp_old_8 
gen risk_14 = gp_old_9 
gen risk_15 = gp_old_10 
gen risk_16 = gp_old_11 
gen risk_17 = gp_old_12 
gen risk_18 = gp_old_13 
gen risk_19 = gp_old_14 
gen risk_20 = gp_old_15 
gen risk_21 = gp_old_16 
gen risk_22 = gp_old_17 
gen risk_23 = dual

label var risk_1 "1(PE)"
label var risk_2 "Age"
label var risk_3 "Female"
label var risk_4 "Black"
label var risk_5 "Married"
label var risk_6 "AMI"
label var risk_7 "Congestive Heart Failure"
label var risk_8 "PVD"
label var risk_9 "CEVD"
label var risk_10 "Dementia"
label var risk_11 "COPD"
label var risk_12 "Rhumatoid Arthirtis"
label var risk_13 "Peptic Ulcer"
label var risk_14 "Mild Liver"
label var risk_15 "Diabetes"
label var risk_16 "Diabetes + Complication"
label var risk_17 "HP/PAPL"
label var risk_18 "Renal"
label var risk_19 "Cancer"
label var risk_20 "Severe Liver"
label var risk_21 "Metastatic Cancer"
label var risk_22 "AIDS"
label var risk_23 "Dual"

eststo clear
eststo : estpost ttest diff_dist_w risk_* ADL_self_initial  alzheimers_initial dementia_initial depression_initial, by(diff_dist_ind) 
esttab using "$Output/Table/4_Balance_Characterisitics.csv" , cells("mu_1(fmt(%9.4f)) N_1(fmt(%9.4gc)) mu_2(fmt(%9.4f)) N_2(fmt(%9.4gc)) p(fmt(%9.4f))") nonumber nodepvar  label replace noobs plain
esttab, cells("mu_1(fmt(%9.4f)) N_1(fmt(%9.4gc)) mu_2(fmt(%9.4f)) N_2(fmt(%9.4gc)) p(fmt(%9.4f))") nonumber nodepvar  label replace noobs



*************************************************
* Robustness Checks - Table 5
*************************************************

*************************************************
// Base Regressions - Table 5.1
*************************************************
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/5_1_Robust_Base.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Varying controls - Table 5.2
************************************************
// No Controls
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq), ///
	absorb(facid bene_hrr#year) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/5_2A_Robust_No_control.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

// Zip Controls
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual ratio_white_ip ratio_renter_ip ratio_below_pvty_ip HH_income_median_ip ma_prop_ind, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/5_2B_Robust_Zip_controls.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

// HSA Controls
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hsa2#year race) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_2C_Robust_HSA_Controls.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Distance Thresholds - Table 5.3
************************************************
// Different Differential Distances
use "$Data/Temp_Files/Person_Robustness",replace

// 15 Miles Threshold
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual if diff_dist_min > -15 & diff_dist_min < 15, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_3A_Robust_Diff_Dist_15.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

// 25 Miles Threshold
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual if diff_dist_min > -25 & diff_dist_min < 25, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_3B_Robust_Diff_Dist_25.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Alternate Functional Form - Table 5.4
************************************************
use "$Data/Temp_Files/Person_Base",replace
egen diff_dist_median = median(diff_dist_mod)
gen dd_ind  = diff_dist_mod <= diff_dist_median
eststo clear
estimates clear
local output2 mortality_90  log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  dd_ind) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_4_Robust_DD_Alternate.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Alternate Clusters - Table 5.5
************************************************
use "$Data/Temp_Files/Person_Base",replace

eststo clear
estimates clear
destring dealid, gen(deal_id)
replace deal_id = facid if missing(deal_id) 
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual , ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(deal_id) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_5_Robust_Deal_Cluster.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Corporatization - Table 5.6
************************************************
// Chain Indicator
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual multifac, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_6A_Robust_Chain.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

// Coefficient for top 5 deals
use "$Data/Temp_Files/Person_Base",replace
destring top5_deal top2_deal bad_pe, replace
// Regressions
gen ind = top5_deal
replace ind = 1 if missing(top5_deal) 
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual if ind == 1 , ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_6B_Robust_top5_only.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 


************************************************
// Excluding Select Deals - Table 5.7
************************************************
// Drop Top2 deals
drop ind
gen ind = top2_deal == 1
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual if ind != 1 , ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
drop ind
esttab reg* using "$Output/Table/5_7A_Robust_top2_remove.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

// Drop Formation Capital
use "$Data/Temp_Files/Person_Base",replace
destring bad_pe, replace
drop if bad_pe == 1
eststo clear
estimates clear
local output2 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual multifac, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_7B_Robust_Formation_Fillmore.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

************************************************
// Retiree Flows - Table 5.8
************************************************
// Drop states with Maximum Inflow and Outflow of retirees
use "$Data/Temp_Files/Person_Base",replace
drop if fac_state == "FL" | fac_state == "AZ" | fac_state == "ID" | fac_state == "NV" | fac_state == "SC" // Inflow
drop if fac_state == "WY" | fac_state == "VT" | fac_state == "NY" | fac_state == "NJ" | fac_state == "IL" // Outflow

eststo clear
estimates clear
local output2 mortality_30 mortality_90 log_pmt log_payment_total_post
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual multifac, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}
esttab reg* using "$Output/Table/5_8_Robust_Drop_Retiree_States_5.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 

*************************************************
* Heterogeneity - Table 6
*************************************************

// Load data
use "$Data/Temp_Files/Person_Base",replace

// Charlson Only
eststo regcharles1: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual  ///
if charles_old == 0, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
estadd scalar FStat = e(first)[4,1]
summ mortality_90 if peind == 0 & charles_old == 0
estadd scalar mean = r(mean)

eststo regcharles2: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual   ///
if charles_old == 1, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
estadd scalar FStat = e(first)[4,1]
summ mortality_90 if peind == 0 & charles_old == 1
estadd scalar mean = r(mean)

// Gender
gen male = female == 0
local outcomes male 
foreach var of local outcomes{
	eststo reg`var'1: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married dual gp_old* ///
	if `var' == 0, absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var' == 0
	estadd scalar mean = r(mean)
	eststo reg`var'2: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married dual gp_old*  ///
	if `var' == 1, absorb(facid bene_hrr#year  race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var' == 1
	estadd scalar mean = r(mean)
}

// Length of Stay
egen los_median = median(length_stay_w)
gen los_ind = length_stay_w > los_median

local outcomes los_ind
foreach var of local outcomes{
	eststo reg`var'1: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual  ///
	if `var' == 0, absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var' == 0
	estadd scalar mean = r(mean)
	eststo reg`var'2: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual  ///
	if `var' == 1, absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var' == 1
	estadd scalar mean = r(mean)
}


// Save
esttab reg* using "$Output/Table/6_Heterogeneity_Patient.csv" , label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc))

// Discharge Related Outcomes
// Discharge Location
use "$Data/Temp_Files/Person_Base",replace
eststo clear
estimates clear

local output2 discharge_home discharge_fac discharge_other
foreach var of local output2{
	eststo reg`var': ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual if `var' == 1, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var' == 1
	estadd scalar mean = r(mean)
}

// Health Diagnosis
eststo reginjury: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual  ///
if Inj_infection == 1, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
estadd scalar FStat = e(first)[4,1]
summ mortality_90 if peind == 0 & Inj_infection == 1
estadd scalar mean = r(mean)

eststo regcardio: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual   ///
if Resp_Circ == 1, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
estadd scalar FStat = e(first)[4,1]
summ mortality_90 if peind == 0 & Resp_Circ == 1
estadd scalar mean = r(mean)

eststo regother: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual ///
if Other == 0, ///
absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
estadd scalar FStat = e(first)[4,1]
summ mortality_90 if peind == 0 & Other == 1
estadd scalar mean = r(mean)

// Save
esttab reg* using "$Output/Table/6_Heterogeneity_Discharge.csv" , label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc))

// Facility Level
// Load data
use "$Data/Temp_Files/Person_Base",replace
// Generate Indicators
sort year facid 
local varlist elder_state logbeds_w adm_bed_w paymcaid_w payprivate_w agg_black_w
foreach var of local varlist{	
	by year: egen `var'_median = median(`var')
	gen `var'_ind = `var' > `var'_median	
}

local varlist urban2 multifac hospbase
foreach var of local varlist{	
	gen `var'_ind = `var' 	
}

// Regressions
eststo clear
local varlist urban2 logbeds_w adm_bed_w
foreach var of local varlist{	
	eststo reg`var'1: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual  ///
	if `var'_ind == 0, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var'_ind == 0
	estadd scalar mean = r(mean)

	eststo reg`var'2: ivreghdfe mortality_90 (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married  gp_old* dual   ///
	if `var'_ind == 1, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ mortality_90 if peind == 0 & `var'_ind == 1
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/6_Heterogeneity_Facility.csv" , label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc))


*************************************************
* Patient Well Being - Table 7
*************************************************
*************************************************
* OLS regressions - Table 7 Panel A
*************************************************
use "$Data/Temp_Files/Person_Base",replace

local output2 mobility_self_worse ulcer_worse pain_intensity_worse

eststo clear
estimates clear

foreach var of local output2{
	eststo reg`var': reghdfe `var' peind c.age#i.sex married gp_old* dual, absorb(facid race  bene_hrr#year) tol(1e-6) accel(sd) cluster(facid)
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

	
esttab reg* using "$Output/Table/7_A_OLS_Wellbeing.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean, labels("Observations" "Y-mean") fmt(%9.3gc %9.4f)) 

*************************************************
* 2SLS regressions - Table 7 Panel B
*************************************************
use "$Data/Temp_Files/Person_Base",replace

local output2 mobility_self_worse ulcer_worse pain_intensity_worse
foreach var of local output2{
	eststo reg`var': ivreghdfe `var' (peind =  diff_dist_mod diff_dist_sq) c.age#i.sex married gp_old* dual, ///
	absorb(facid bene_hrr#year race) tol(1e-6) accel(sd) cluster(facid) savefirst 
	estadd scalar FStat = e(first)[4,1]
	summ `var' if peind == 0
	estadd scalar mean = r(mean)
}

esttab reg* using "$Output/Table/7_B_IV_Wellbeing.csv", label keep(peind) star(* 0.10 ** 0.05 *** 0.01) nonotes plain ///
se b(%9.4f) replace stats(N mean FStat, labels("Observations" "Y-mean" "First Stage F Stat") fmt(%9.3gc %9.4f %9.4gc)) 


*************************************************
* Mechanisms and Operational Changes - Table 8
************************************************
use "$Data/Temp_Files/Facility_Base",replace

est clear 
set more off
local outcomes rating_overall_w rating_survey_w staff_hr_by_resday_w cna_hr_by_resday_w lpn_hr_by_resday_w rn_hr_by_resday_w lman_fee_all_totalxw llease_all_totalxw lint_expense_all_totalxw
 
foreach depvar of local outcomes {
	eststo: reghdfe `depvar' peind , absorb(year facid) cluster(facid)
			summ `depvar' if peind==0
			estadd scalar mean = r(mean)
			
	eststo: reghdfe `depvar' peind agg_cmi_w acuityindx_w agg_black_w la_herfbeds_cty res_profit_cty res_multifac_cty res_hospbase_cty logcount , absorb(year facid) cluster(facid)
			summ `depvar' if peind==0
			estadd scalar mean = r(mean)		
		}	
					 
esttab  est* using "$Output/Table/8_Facility_Results.csv", keep(peind) replace label csv se(%9.3f) b(%9.3f) star(* 0.10 ** 0.05 *** 0.01) r2(%9.3f) nogaps scalars("mean Outcome Mean")


